import json
from rest_framework import serializers
from ..models.Author import Author
from ..utils.literUtils import emptyValue

# 查询内容序列化
class AuthorListQuerySerializer(serializers.Serializer):
    name = serializers.CharField(required=False,allow_blank=True)
    zh_name = serializers.CharField(required=False,allow_blank=True)
    orcid = serializers.CharField(required=False,allow_blank=True)
    email = serializers.CharField(required=False,allow_blank=True)
    address = serializers.CharField(required=False,allow_blank=True)
    country = serializers.CharField(required=False,allow_blank=True)
    city = serializers.CharField(required=False,allow_blank=True)
    phone = serializers.CharField(required=False,allow_blank=True)
    education_degree = serializers.CharField(required=False,allow_blank=True)
    research_field = serializers.CharField(required=False,allow_blank=True)
    cited_count = serializers.CharField(required=False,allow_blank=True)
    homepage = serializers.CharField(required=False,allow_blank=True)
    description = serializers.CharField(required=False,allow_blank=True)
    tag = serializers.CharField(required=False,allow_blank=True)
    institutions = serializers.CharField(required=False,allow_blank=True)
    departments = serializers.CharField(required=False,allow_blank=True)

    page_size = serializers.IntegerField(
        required=False, max_value=50, min_value=1)
    current_page = serializers.IntegerField(required=False)
    sort_prop = serializers.CharField(required=False,allow_blank=True)
    sort_order = serializers.CharField(required=False,allow_blank=True)

    def validate(self, attrs):
        filter_flag = True
        page_flag = True
        if "name" in attrs:
            attrs["name"] = attrs["name"].strip()
            if attrs["name"] == "": attrs.pop("name")
        if "zh_name" in attrs:
            attrs["zh_name"] = attrs["zh_name"].strip()
            if attrs["zh_name"] == "": attrs.pop("zh_name")
        if "orcid" in attrs:
            attrs["orcid"] = attrs["orcid"].strip()
            if attrs["orcid"] == "": attrs.pop("orcid")
        if "email" in attrs:
            attrs["email"] = attrs["email"].strip()
            if attrs["email"] == "": attrs.pop("email")
        if "address" in attrs:
            attrs["address"] = attrs["address"].strip()
            if attrs["address"] == "": attrs.pop("address")
        if "country" in attrs:
            attrs["country"] = attrs["country"].strip()
            if attrs["country"] == "": attrs.pop("country")
        if "city" in attrs:
            attrs["city"] = attrs["city"].strip()
            if attrs["city"] == "": attrs.pop("city")
        if "phone" in attrs:
            attrs["phone"] = attrs["phone"].strip()
            if attrs["phone"] == "": attrs.pop("phone")
        if "education_degree" in attrs:
            attrs["education_degree"] = attrs["education_degree"].strip()
            if attrs["education_degree"] == "": attrs.pop("education_degree")
        if "research_field" in attrs:
            attrs["research_field"] = attrs["research_field"].strip()
            if attrs["research_field"] == "": attrs.pop("research_field")
        if "cited_count" in attrs:
            attrs["cited_count"] = attrs["cited_count"].strip()
            if attrs["cited_count"] == "": attrs.pop("cited_count")
        if "homepage" in attrs:
            attrs["homepage"] = attrs["homepage"].strip()
            if attrs["homepage"] == "": attrs.pop("homepage")
        if "description" in attrs:
            attrs["description"] = attrs["description"].strip()
            if attrs["description"] == "": attrs.pop("description")
        if "tag" in attrs:
            attrs["tag"] = attrs["tag"].strip()
            if attrs["tag"] == "": attrs.pop("tag")
        if "institutions" in attrs:
            attrs["institutions"] = attrs["institutions"].strip()
            if attrs["institutions"] == "": attrs.pop("institutions")
        if "departments" in attrs:
            attrs["departments"] = attrs["departments"].strip()
            if attrs["departments"] == "": attrs.pop("departments")
        if ("name" not in attrs or attrs["name"] == "") and ("zh_name" not in attrs or attrs["zh_name"] == "") and\
           ("orcid" not in attrs or attrs["orcid"] == "") and ("email" not in attrs or attrs["email"] == "")  and\
           ("country" not in attrs or attrs["country"] == "") and ("city" not in attrs or attrs["city"] == "") and\
           ("address" not in attrs or attrs["address"] == "") and ("phone" not in attrs or attrs["phone"] == "") and\
           ("education_degree" not in attrs or attrs["education_degree"] == "") and ("research_field" not in attrs or attrs["research_field"] == "") and\
           ("cited_count" not in attrs or attrs["cited_count"] == "") and ("homepage" not in attrs or attrs["homepage"] == "") and\
           ("description" not in attrs or attrs["description"] == "") and ("tag" not in attrs or attrs["tag"] == "") and\
           ("institutions" not in attrs or attrs["institutions"] == "") and ("departments" not in attrs or attrs["departments"] == ""):
            filter_flag = False
        if ("page_size" not in attrs):
            page_flag = False
        else:
            if "current_page" not in attrs:
                attrs["current_page"] = 1
        if not filter_flag and not page_flag:
            raise serializers.ValidationError({
                'detail': '查询条件过于宽泛'
            })
        try:
            attrs = super().validate(attrs)
        except serializers.ValidationError as e:
            raise serializers.ValidationError({
                'detail': '验证失败:' + str(e.detail)
            })
        return attrs
    
    @classmethod
    def get_query_fields(self):
        return [{"title":"名字：", "dataIndex":"name", "type":"CharField"},
                {"title":"中文名：", "dataIndex":"zh_name", "type":"CharField"},
                {"title":"orcid：", "dataIndex":"orcid", "type":"CharField"},
                {"title":"邮箱：", "dataIndex":"email", "type":"CharField"},
                {"title":"详细地址：", "dataIndex":"address", "type":"CharField"},
                {"title":"所在国家：", "dataIndex":"country", "type":"CharField"},
                {"title":"所在城市：", "dataIndex":"city", "type":"CharField"},
                {"title":"电话号码：", "dataIndex":"phone", "type":"CharField"},
                {"title":"学历或职称信息：", "dataIndex":"education_degree", "type":"CharField"},
                {"title":"研究领域描述：", "dataIndex":"research_field", "type":"CharField"},
                {"title":"文章被引用数：", "dataIndex":"cited_count", "type":"CharField"},
                {"title":"个人主页链接：", "dataIndex":"homepage", "type":"CharField"},
                {"title":"作者简介：", "dataIndex":"description", "type":"CharField"},
                {"title":"标签：", "dataIndex":"tag", "type":"ManyToManyField"},
                {"title":"所属机构：", "dataIndex":"institutions", "type":"ManyToManyField"},
                {"title":"所属部门：", "dataIndex":"departments", "type":"ManyToManyField"},]
        
        
# 用于输出查询时的数据


class AuthorListSerializer(serializers.ModelSerializer):
    institutions = serializers.SerializerMethodField()
    departments = serializers.SerializerMethodField()
    tag = serializers.SerializerMethodField()

    class Meta:
        model = Author
        fields = ['uuid', 'name', 'zh_name', "orcid", "email", 'address', 'country', 'city', 'phone', "education_degree", \
                  "research_field", 'cited_count', "homepage", 'description', 'tag', "institutions", "departments"]
    
    def get_institutions(self, obj):
        institutions_content = ""
        for institution in obj.institutions.all():
            if emptyValue(institution.zh_name):
                institutions_content += institution.name + "; "
            else:
                institutions_content += institution.name + ' (' + institution.zh_name + '); '
        return institutions_content[:-2]
    
    def get_tag(self, obj):
        tags_content = ""
        for tag in obj.tag.all():
            if emptyValue(tag.v):
                tags_content += tag.k + '; '
            else:
                tags_content += tag.k + ' (' + tag.v + '); '
        return tags_content[:-2]
    
    def get_departments(self, obj):
        departments_content = ""
        for department in obj.departments.all():
            if emptyValue(department.zh_name):
                departments_content += department.name + "; "
            else:
                departments_content += department.name + ' (' + department.zh_name + '); '
        return departments_content[:-2]
        
    @classmethod
    def get_extra_fields_info(self):
        return [{"title":"标签", "dataIndex":"tag", "type":"CharField"},
                {"title":"所属机构", "dataIndex":"institutions", "type":"CharField"},
                {"title":"所属部门", "dataIndex":"departments", "type":"CharField"},]